<?php

/**
 * forumTopic actions.
 *
 * @package    theconnexter
 * @subpackage forumTopic
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class forumTopicActions extends sfActions
{
  public function executeIndex(sfWebRequest $request)
  {
    $this->forum_topics = Doctrine::getTable('forumTopic')
      ->createQuery('a')
      ->execute();
	$this->forum_topics = Doctrine::getTable('forumTopic')->getActiveTopics2();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->forum_topic = Doctrine::getTable('forumTopic')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->forum_topic);
	$this->forum_category = Doctrine::getTable('forumCategory')->find($this->forum_topic->getCategoryId());
	$this->forum_members = Doctrine::getTable('forumMembers')->getForumMember($this->forum_topic->getUserId());
	$this->forum_members = $this->forum_members[0];
	$this->forum_posts  = Doctrine::getTable('forumPost')->getPosts($this->forum_topic->getId());
	
	if ($this->getUser()->isAuthenticated())
	{
		$userId = $this->getUser()->getGuardUser()->getId() ;
		//$this->isallows = Doctrine::getTable('forumMembers')->findByUserIdAndIsActive($userId,true)->count();
                $members = Doctrine::getTable('forumMembers')->findByUserId($userId);
                $member = $members[0];
                $userId = $member->getId() ;
                if(empty( $userId)) {
                    //pas inscript au forum
                     $this->isallows = 2;
                }
                else {                  
                    if(!$member->getIsActive()) {
                        //inscript et non encore validé
                        $this->isallows = 3;
                    }
                    else {
                        //autorisation à participation
                       $this->isallows = 4;
                    }
                }

	}
	else {
         // utilisateur non identifié
            $this->isallows = 1;
        }
	
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new forumTopicForm();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new forumTopicForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($forum_topic = Doctrine::getTable('forumTopic')->find(array($request->getParameter('id'))), sprintf('Object forum_topic does not exist (%s).', $request->getParameter('id')));
    $this->form = new forumTopicForm($forum_topic);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($forum_topic = Doctrine::getTable('forumTopic')->find(array($request->getParameter('id'))), sprintf('Object forum_topic does not exist (%s).', $request->getParameter('id')));
    $this->form = new forumTopicForm($forum_topic);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($forum_topic = Doctrine::getTable('forumTopic')->find(array($request->getParameter('id'))), sprintf('Object forum_topic does not exist (%s).', $request->getParameter('id')));
    $forum_topic->delete();

    $this->redirect('forumTopic/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $forum_topic = $form->save();

      $this->redirect('forumTopic/edit?id='.$forum_topic->getId());
    }
  }
  
  public function executePostCreate(sfWebRequest $request)
  {
  
    $forum_topics = new forumPost();
	$topicId = $request->getParameter("topicId");
	$messsage = $request->getParameter("messsage");
	$topicId = $request->getParameter("topicId");
	$forum_topics->setIsActive(false);
	$forum_topics->setTopicId($topicId);
	$forum_topics->setMessage($messsage);
	if (!$this->getUser()->isAuthenticated())
	{
	  $this->getUser()->setFlash('notice','Vous devez  être connectés pour publier sur ce forum');
	  $this->redirect('@homepage');
	}
	
	$forum_topics->setUserId($this->getUser()->getGuardUser()->getId());
	
	$forum_topics->save();
	
	$this->redirect('@homepage');
   
  }

  public function executePostAskAuthorisation(sfWebRequest $request)
  {

      $forumMembers = new forumMembers();
      $topicId = $request->getParameter('topicId');
      if ($this->getUser()->isAuthenticated())
	{
            $userId = $this->getUser()->getGuardUser()->getId() ;
            $forumMembers->setIsActive(false);
            $forumMembers->setUserId($userId);
            
             $count =  Doctrine::getTable('forumMembers')->findByUserId($userId)->count();
            if($count == 0) {
                //verifie que l'utilisateur na pas encore fais de demande
                $forumMembers->save();
            }
	}
	
	$this->redirect('@show_forum_topic?id='.$topicId);

  }
}
